package com.snopy.leetcode.index1_1000.index1_100;

/**
 * @author snopy
 * @version 1.0.0
 * @classname Question48
 * @description 旋转图形
 *
 *
 * @email 77912204@qq.com
 * @date 2022/04/11 23:17
 */
public class Question48 {
    public static void main(String[] args) {
        int[][] matrix = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
        rotate2(matrix);
        for (int[] ints : matrix) {
            for (int anInt : ints) {
                System.err.print(anInt+"\t");
            }
            System.err.println();
        }
    }
    /**
     * @Description
     *  1  2  3    7  4  1
     *  4  5  6 -> 8  5  2
     *  7  8  9    9  6  3
     *  0,0  ->  0,2
     *  0,1  ->  1,2
     *  0,2  ->  2,2
     *
     *  1,0  ->  0,1
     *  1,1  ->  1,1
     *  1,2  ->  2,1
     *
     *  2,0  ->  0,0
     *  2,1  ->  1,0
     *  2,2  ->  2,0
     *  (n-i1,i2)
     * @param matrix:
     * @return: void
     * @Date 2022-04-11
     * @Author txl77912204@gmail.com
     **/
    public static void rotate(int[][] matrix) {
        int len = matrix.length;
        int[][] matrix_new = new int[len][len];
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                matrix_new[j][len-1-i]=matrix[i][j];
            }
        }
        for (int i = 0; i < len; i++) {
            System.arraycopy(matrix_new[i], 0, matrix[i], 0, len);
        }
    }
    /**
     * @Description 原地旋转
     *
     * matrix[col][n-row-1]=matrix[row][col]
     * row = col
     * col = n-row-1
     *
     * matrix[n-row-1][n-col-1]=matrix[col][n-row-1]
     * row = n-row-1
     * col = n-col-1
     *
     * matrix[n-col-1][row]=matrix[n-row-1][n-col-1]
     *
     * row = n-col-1
     * col = row
     * matrix[row][col] = matrix[n-col-1][row]
     *
     * @param matrix:
     * @return: void
     * @Date 2022-04-12
     * @Author txl77912204@gmail.com
     **/
    public static void rotate2(int[][] matrix) {
        int len = matrix.length;
        for (int row = 0; row < len/2; row++) {
            for (int col = 0; col < (len+1)/2; col++) {
                int tmp = matrix[row][col];
                matrix[row][col] = matrix[len-col-1][row];
                matrix[len-col-1][row]=matrix[len-row-1][len-col-1];
                matrix[len-row-1][len-col-1]=matrix[col][len-row-1];
                matrix[col][len-row-1]=tmp;
            }
        }
    }

}
